<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // https://upload-images.jianshu.io/upload_images/1726554-156436f8dda59bf9.png?imageMogr2/auto-orient/strip|imageView2/2/w/966/format/webp
    var arr = [3, 5, 7, 1, 4, 56, 12, 78, 25, 0, 9, 8, 42, 37];
    // 插入排序
    function Insertion(arr) {
        let len = arr.length;
        let preIndex, current;
        for (let i = 1; i < len; i++) {
            preIndex = i - 1;
            current = arr[i];
            while (preIndex >= 0 && current < arr[preIndex]) {
                arr[preIndex + 1] = arr[preIndex];
                preIndex--;
            }
            arr[preIndex + 1] = current;
        }
        return arr;
    }
    Insertion(arr)



    // 选择排序
    function selectionSort(arr) {
        var len = arr.length;
        var minIndex, temp;
        for (var i = 0; i < len - 1; i++) {
            minIndex = i;
            for (var j = i + 1; j < len; j++) {
                if (arr[j] < arr[minIndex]) {     // 寻找最小的数
                    minIndex = j;                 // 将最小数的索引保存
                }
            }
            temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
        return arr;
    }

    // ------------------ 归并排序 ------------------
    //  递归 & 归并
    function mergeSort(arr) {
        let len = arr.length
        if (len < 2) {
            return arr
        }
        let middle = Math.floor(len / 2)
        //拆分成两个子数组
        let left = arr.slice(0, middle)
        let right = arr.slice(middle, len)
        //递归拆分
        let mergeSortLeft = mergeSort(left)
        let mergeSortRight = mergeSort(right)
        //合并
        return merge(mergeSortLeft, mergeSortRight)
    }
    const merge = (left, right) => {
        const result = [];
        while (left.length && right.length) {
            // 注意: 判断的条件是小于或等于，如果只是小于，那么排序将不稳定.
            if (left[0] <= right[0]) {
                result.push(left.shift()); //每次都要删除left或者right的第一个元素，将其加入result中
            } else {
                result.push(right.shift());
            }
        }
        //将剩下的元素加上
        while (left.length) result.push(left.shift());
        while (right.length) result.push(right.shift());
        return result;
    };
    // ------------------ 归并排序 ------------------



    // ------------------ 快速排序 ------------------
    // 
    // var arr = [3, 5, 7, 1, 4, 56, 12, 78, 25, 0, 9, 8, 42, 37];
    var quickSort = function (arr) {
        if (arr.length <= 1) {
            return arr;
        }
        var pivotIndex = Math.floor(arr.length / 2);
        var pivot = arr.splice(pivotIndex, 1)[0];
        var left = [];
        var right = [];

        for (var i = 0; i < arr.length; i++) {
            if (arr[i] < pivot) {
                left.push(arr[i]);
            } else {
                right.push(arr[i]);
            }
        }
        return quickSort(left).concat([pivot], quickSort(right));
    };
    console.log('quickArr: ', quickSort(arr))
    // ------------------ 快速排序 ------------------

    // 原地快速排序 不需要申请新的内存
    
    // 原地快速排序 不需要申请新的内存

    // 桶排序

    // 计数排序 保证稳定性
    // 2，5，3，0，2，3，0，3

    // 基数排序 ，对十万个手机号进行排序



</script>

</html>